<?php
namespace Yzadmin\Controller;
use Common\Controller\AdminBaseController;
/**
 * 后台管理员控制器
 */
class AdminController extends AdminBaseController {

  public function index(){
    $list = D('admin')->where('status=0')->select();
    $assign=array(
        'lists'=>$list
    );
    $this->assign($assign);
    $this->display();
  }

  public function add(){
    if (IS_POST){
      $data = I('post.data');
      // if(empty($id)){$this->error('服务器正忙，请稍后再试！');}
      $data['userpwd'] = md5($data['userpwd']);
      $data['clearpwd'] = $data['userpwd'];
      $data['create_time'] = time();
      $last_id = D('Admin')->add($data);
      $AuthGroup = D('Auth_group_access')->where(array('uid'=>$last_id))->find();
      $datas['uid'] = $last_id;$datas['group_id'] = $data['catid'];
      if($AuthGroup){
        D('Auth_group_access')->where(array('uid'=>$last_id))->save($datas);
      }else{
        D('Auth_group_access')->add($datas);
      }
      if(false !== $last_id){
        $this->success('数据操作成功！',U('index'));exit;
      }else{
        $this->error('服务器正忙，请稍后再试！');
      }
    }
    $assign=array(
      'cateType'=>D('AuthGroup')->select()
    );
    $this->assign($assign);
    $this->display();
  }

  public function editpass(){
    if (IS_POST){
      $data = I('post.data');$id = intval(I('post.id'));
      if(empty($id)){$this->error('服务器正忙，请稍后再试！');}
      $data['userpwd'] = md5($data['userpwd']);
      $data['clearpwd'] = $data['userpwd'];
      $result = D('Admin')->where(array('id'=>$id))->save($data);
      if(false !== $result){
        $this->success('数据操作成功！',U('index'));exit;
      }else{
        $this->error('服务器正忙，请稍后再试！');
      }
    }
    $id=I('get.id');if (empty($id)){$this->error('非法操作');}
    $info=D('Admin')->find($id);
    $assign=array(
      'cateType'=>D('AuthGroup')->select(),
      'info'=>$info,
      'id'=>$id
    );
    $this->assign($assign);
    $this->display();
  }

  public function del($id = 0){
    if(IS_POST){
      if(empty($id)){echo 0;exit;}
      $result2 = D('Auth_group_access')->where('uid='.$id)->delete();
      $result = D('Admin')->where('id='.$id)->delete();
      if($result){
        echo 1;exit;
      }else{
        echo 0;exit;
      }
    }
  }

  public function userinfo(){
    if (IS_POST){
      $data = I('post.data');$id = intval(I('post.id'));
      if(empty($id)){$this->error('服务器正忙，请稍后再试！');}
      $AuthGroup = D('Auth_group_access')->where(array('uid'=>$id))->find();
      $datas['uid'] = $id;$datas['group_id'] = $data['catid'];
      if($AuthGroup){
        D('Auth_group_access')->where(array('uid'=>$id))->save($datas);
      }else{
        D('Auth_group_access')->add($datas);
      }
      $result = D('Admin')->where(array('id'=>$id))->save($data);
      if(false !== $result){
        $this->success('数据操作成功！',U('index'));exit;
      }else{
        $this->error('服务器正忙，请稍后再试！');
      }
    }
    $id=I('get.id');
    if (empty($id)){$this->error('非法操作');}
    $info=D('Admin')->find($id);
    $assign=array(
      'cateType'=>D('AuthGroup')->select(),
      'info'=>$info,
      'id'=>$id
    );
    $this->assign($assign);
    $this->display();
  }


}
